<?php

class User_Form_ForgetPasswordForm extends Ac_Form
{
    protected $formDecorators = array(
            'FormElements',
            //array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
            //array('Description', array('placement' => 'prepend')),
            'fieldset',
            'Form'
    );


    protected $elementDecorators = array(
        'ViewHelper',
        'Errors',
       // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        array(array('row' => 'HtmlTag'), array('tag' => 'p', 'class'=>'username text')),
        array('Label', array('placement' => 'prepend')),
        array('Description', array('placement' => 'prepend')),
        
        //array('HtmlTag', array('class' => 'inputText')),
    );

    protected $elementButtonDecorators = array(
        'ViewHelper',
        'Errors',
        //array(array('row' => 'HtmlTag'), array('tag' => 'button'))
       // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        array(array('row' => 'HtmlTag'), array('tag' => 'p', 'class'=>'button_em')),
        //array('HtmlTag', array('class' => 'inputText')),
    );

    public function init()
    {
        parent::init();

        /* Credential Error */
        $this->addElement('hidden', 'phone', array(
            'filters'     => array('StringTrim', 'StringToLower'),
            'id'          => 'phone',
            'name'        => 'phone',
            'description' => '请输入你登录猫找网的邮箱地址并按下‘重置密码’按钮，新的密码将会发送至你的邮箱。',
            'validators'  => array(
                'Int',
                array('StringLength', false, array(3, 20)),
            ),
            'decorators' => array(
                'ViewHelper',
                //'Errors',
                array('errors', array('escape' => false, 'placement' => 'APPEND', 'tag' => 'div', 'class'=>'errors')),
                array('description', array('escape' => false, 'placement' => 'PREPEND', 'tag' => 'p', 'class'=>'desc')),                            
                //array('label', array('escape' => false, 'placement' => 'PREPEND', 'tag' => 'div', 'id' => 'error_login', 'style' => 'display:none')),
                
                //array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'desc errors rachel_copy')),
            ),
            'required'   => false,
        ));

        $username = $this->addElement('text', 'email', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'EmailAddress',
            ),
            'decorators' => $this->elementDecorators,
            //'description' => 'Enter your email below and click Reset Password. We will send a new password to you. ',
            'required'   => true,
            'label'      => 'Email Address',
            'decorators' => array(
                            'ViewHelper',
                            'Errors',
                            array('label', array('escape' => false, 'placement' => 'PREPEND')),
                            //array('description', array('escape' => false, 'placement' => 'PREPEND', 'tag' => 'div', 'class'=>'desc')),
                            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'email text')),
            ),
            'validators' => array(
                array('NotEmpty', true, array('messages' => 'This information is required')),
                array('EmailAddress', true, array('messages' => array(Zend_Validate_EmailAddress::INVALID_FORMAT => 'Valid email required'))),
            ),
        ));


        $login = $this->addElement('button', 'login', array(
            'required' => false,
            'ignore'   => true,
            'type'       =>'submit',
            'decorators' => $this->elementButtonDecorators,
            'label'      => '重置密码',
            'decorators' => array(
                            'ViewHelper',
                            'Errors',
                            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'button em')),
            ),
        ));
        //$this->setLegend('Reset Your Password');
    }

    public function isValid($arr = array())
    {
        $test = parent::isValid($arr);

        if ($test == true) {
            $user   = Model_User::one(array('email' => $arr['email']));

            if (!empty($arr['email']) && empty($user)) {
                $this->getElement('phone')->setErrors(array('<div class="errors"><p><em>Incorrect Email Address</em></p><p>'.$this->view->t('The email address entered is not registered on AsiaClassified. Please try again or <a href="/user/register">Sign Up</a> now.</p></div>')));
                $test = false;
            }
        }
        return $test;
    }

    public function save()
    {
        //send the message
        $subject = $this->view->t('重置你的猫找网密码');
        $user    = Model_User::one(array('email' => $this->getValue('email')));
        if (!empty($user)) {
            $email   = $user->email;
            $pw = Model_User::getNewPassword();
            $user->password = Model_User::generatePassword($pw);
            $user->save();
            $message = '';

            // if (APPLICATION_ENV == 'development') {
                // $email = 'olivier@olivier-desktop';
            // }

            Ac_Util::sendEmail($subject, $message, $email, true, array('user' => $user, 'password' => $pw), Ac_Mail::TEMPLATE_FORGOTPASSWORD);
        }
    }
}

